package com.wuhuiyue.controller;

import cn.hutool.json.JSON;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.wuhuiyue.entity.Result;
import com.wuhuiyue.entity.Software;
import com.wuhuiyue.service.ISoftwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author hy woo
 * @date 2022/6/11 17:10
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    ISoftwareService softwareService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile[] file) throws IOException {
        for (MultipartFile multipartFile : file) {
            multipartFile.transferTo(new File("d:/test/"+multipartFile.getOriginalFilename()));
// 写法1：JDK8+ ,不用额外写一个DemoDataListener
            // since: 3.0.0-beta1
            String fileName = "d:/test/" + multipartFile.getOriginalFilename();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            // 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行
            EasyExcel.read(fileName, Software.class, new PageReadListener<Software>(dataList -> {
                for (Software demoData : dataList) {
                    softwareService.save(demoData);
                }
            })).sheet().doRead();
        }
        return Result.OK();
    }
}
